<?php

/**
 * This is the model class for table "{{unitprojects}}".
 *
 * The followings are the available columns in table '{{unitprojects}}':
 * @property integer $id
 * @property string $create_at
 * @property string $modify_at
 * @property string $create_user_id
 * @property string $update_user_id
 * @property integer $project_id
 * @property string $name
 * @property string $description
 * @property string $pre_start_at
 * @property string $pre_end_at
 * @property string $real_start_at
 * @property string $real_end_at
 * @property string $type
 * @property string $appearance_score_total
 * @property string $appearance_score_got
 * @property integer $divide_projects_status
 * @property integer $end_work_status
 * @property string $quality_file
 *
 * The followings are the available model relations:
 * @property Projects $project
 */
class UnitProject extends CActiveRecord
{
	public $tempFile;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UnitProject the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{unitprojects}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('project_id, name', 'required'),
			array('project_id, divide_projects_status, end_work_status, appearance_score_total, appearance_score_got,', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>48),
			array('description, type', 'length', 'max'=>255),
			array('appearance_score_total, appearance_score_got, quality_file', 'length', 'max'=>5),
			array('create_user_id, update_user_id', 'length', 'max'=>10),
			array('modify_at, pre_start_at, pre_end_at, real_start_at, real_end_at', 'safe'),
			array('tempFile', 'file', 'types'=>'pdf, doc, docx, pages', 'safe'=>true, 'allowEmpty' => true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, create_at, modify_at, create_user_id, update_user_id, project_id, 
					name, description, pre_start_at, pre_end_at, real_start_at, real_end_at,
					type, appearance_score_total, appearance_score_got, divide_projects_status, end_work_status, quality_file', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'segmentProjects' => array(self::HAS_MANY, 'SegmentProject', 'unitproject_id'),
			'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'create_at' => Yii::t('common','Create At'),
			'modify_at' => Yii::t('common','Modify At'),
			'create_user_id' => 'Create User',
			'update_user_id' => 'Update User',
			'project_id' => Yii::t('project','Belonging Project'),
			'name' => Yii::t('project','Unit Project Name'),
			'description' => Yii::t('project','Unit Project Description'),
			'type' => Yii::t('project','Unit Project Type'),
			'appearance_score_total' => Yii::t('project','Appearance Score Total'),
			'appearance_score_got' => Yii::t('project','Appearance Score Got'),
			'pre_start_at' => Yii::t('project','Pre Start At'),
			'pre_end_at' => Yii::t('project','Pre End At'),
			'real_start_at' => Yii::t('project','Real Start At'),
			'real_end_at' => Yii::t('project','Real End At'),
			'quality_file' => Yii::t('project','Unit Project Quality File'),
			'tempFile' => Yii::t('project','Unit Project Quality File'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('create_at',$this->create_at,true);
		$criteria->compare('modify_at',$this->modify_at,true);
		$criteria->compare('create_user_id',$this->create_user_id,true);
		$criteria->compare('update_user_id',$this->update_user_id,true);
		$criteria->compare('project_id',$this->project_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('pre_start_at',$this->pre_start_at,true);
		$criteria->compare('pre_end_at',$this->pre_end_at,true);
		$criteria->compare('real_start_at',$this->real_start_at,true);
		$criteria->compare('real_end_at',$this->real_end_at,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('appearance_score_total',$this->appearance_score_total,true);
		$criteria->compare('appearance_score_got',$this->appearance_score_got,true);
		$criteria->compare('divide_projects_status',$this->divide_projects_status);
		$criteria->compare('end_work_status',$this->end_work_status);
		$criteria->compare('quality_file',$this->quality_file);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	protected function beforeSave()
	{
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->create_at = date('Y-m-d H:i:s');
				$this->modify_at = date('Y-m-d H:i:s');
				$this->create_user_id = Yii::app()->user->id;
				$this->update_user_id = Yii::app()->user->id;
			}else{
				$this->modify_at = date('Y-m-d H:i:s');
				$this->update_user_id = Yii::app()->user->id;
			}
			return true;
		}else{
			return false;
		}
	}
}